package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;

import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.TypedModel;
import edu.pku.sei.transformation.editor.views.figures.TagedElementFigure;

public class TypedModelNodeEditpart extends NodeEditpart {

	public TypedModelNodeEditpart(MetaShapeElement model) {
		super(model);
	}

	@Override
	protected IFigure createFigure() {
		TagedElementFigure figure = new TagedElementFigure();
		TypedModel p = (TypedModel) getNode().getModelElement();
		figure.setName(p.toString());
		figure.setTag("<<Typed Model>>");
		return figure;
	}
	
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		TagedElementFigure figure = (TagedElementFigure) getFigure();
		TypedModel p = (TypedModel) getNode().getModelElement();
		if (ModelPackage.NAMEDELEMENT_NAME.equals(propertyName)) {
			figure.setName(p.toString());
		} else if (ModelPackage.TYPEDELEMENT_TYPE.equals(propertyName)) {
			figure.setName(p.toString());
		} else {
			super.handlePropertyChanged(event);			
		}
	}
	
	@Override
	protected void refreshVisuals() {
		TagedElementFigure figure = (TagedElementFigure) getFigure();
		TypedModel p = (TypedModel) getNode().getModelElement();
		figure.setName(p.toString());
		super.refreshVisuals();
	}
	
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		if (getNode().isReferenceShape())
			installEditPolicy(EditPolicy.COMPONENT_ROLE, null);
	}
	
	

}
